//
//  TTButton.m
//  TooToo
//
//  Created by liuning on 15/10/27.
//  Copyright © 2015年 MoHao. All rights reserved.
//

#import "TTButton.h"

@interface TTButton ()
{
    TTButtonStyle btnStyle;
}
@property (nonatomic,strong)TapBlock tapBlock;

@end

@implementation TTButton

-(void)setTitle:(NSString *)title style:(TTButtonStyle)style
{
    [self setTitle:title forState:UIControlStateNormal];
    self.titleLabel.font = [UIFont systemFontOfSize:14];
    
    btnStyle = style;
    self.canTap = YES;
    
    switch (btnStyle) {
            
        case TTButtonStyleRedCorner:
        {
            [self setTitleColor:FBEE_COLOR_TEXT forState:UIControlStateNormal];
            
            break;
        }
        
        case TTButtonStyleRedCobe:
        {

            [self setBackgroundColor:FBEE_COLOR_THEME];
            [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            
            break;
        }
            
        case TTButtonStyleRedEdge:
        {
            [self setBackgroundImage:[UIImage imageNamed:@"redEdge_btn_highlight"] forState:UIControlStateHighlighted];
            self.layer.borderWidth = 1;
            self.clipsToBounds = YES;
            
            break;
        }
            
        case TTButtonStyleGrayEdge:
        {
            [self setTitleColor:FBEE_COLOR_BACKGROUND forState:UIControlStateNormal];
            [self setBackgroundImage:[UIImage imageNamed:@"grayEdge_btn_highlight"] forState:UIControlStateHighlighted];
            self.layer.borderColor = FBEE_COLOR_BACKGROUND.CGColor;
            self.layer.borderWidth = 1;
            self.clipsToBounds = YES;
            
            break;
        }
        
        case TTButtonStyleGrayText:
        {
            self.backgroundColor = [UIColor clearColor];
            [self setTitleColor:FBEE_COLOR_TEXT forState:UIControlStateNormal];
            [self setTitleColor:FBEE_COLOR_TEXT forState:UIControlStateHighlighted];
            break;
        }
            
        case TTButtonStyleNone:
        {
            break;
        }
        
        default:
            break;
    }
}
-(void)setTitle:(NSString *)title style:(TTButtonStyle)style tapped:(TapBlock)block
{
    [self setTitle:title style:style];
    [self addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.tapBlock = block;
}
-(void)setTitle:(NSString *)title style:(TTButtonStyle)style target:(id)target buttonAction:(SEL)action
{
    [self setTitle:title style:style];
    [self addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
}

-(void)setCanTap:(BOOL)canTap
{
    _canTap = canTap;
    
    if (canTap) {
        
        self.selected = NO;
        self.userInteractionEnabled = YES;
        
    }else{
        self.selected = YES;
        self.userInteractionEnabled = NO;
    }
    
    switch (btnStyle) {
            
        case TTButtonStyleRedEdge:
        {
            if (canTap) {
                [self setTitleColor:FBEE_COLOR_THEME forState:UIControlStateNormal];
                self.layer.borderColor = FBEE_COLOR_THEME.CGColor;
            }else{
                [self setTitleColor:COLOR_TEXT_GRAY forState:UIControlStateNormal];
                self.layer.borderColor = COLOR_TEXT_GRAY.CGColor;
            }
            
            break;
        }
            
        case TTButtonStyleRedCorner:
        {
            if (canTap) {
                
                [self setBackgroundColor:FBEE_COLOR_BACKGROUND];
            }else{
                [self setBackgroundColor:UIColorFromRGB(0xdbdbdb)];
            }
            
            break;
        }
        case TTButtonStyleRedCobe:
        {
            
            if (canTap) {
                [self setBackgroundColor:FBEE_COLOR_THEME];

            }
            else
            {
                [self setBackgroundColor:COLOR_PLACEHODER];

            }
            break;
        }
            
        default:
            break;
    }
}

-(void)buttonAction:(UIButton *)sender
{
    self.tapBlock();
}

@end
